package com.PVZ.controller;

import com.PVZ.engine.AnimeControl;
import com.PVZ.engine.Cleaner;
import com.PVZ.engine.Judgement;
import com.PVZ.engine.Stage;
import com.PVZ.model.Entity;
import com.PVZ.model.weapon.Weapon;
import com.PVZ.utils.ThreadPool;
import com.PVZ.utils.TimeTaskPool;
import com.PVZ.view.CardView;
import com.PVZ.view.EntityView;
import com.PVZ.view.GameFrame;
import com.PVZ.view.canvas.EntityCanvas;
import lombok.Data;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by @Author: kabuda on @Date: 2021/5/18 23:05
 */
@Data
public class GameController {
    private GameFrame gameFrame;
    private EntityCanvas entityCanvas;
    private Stage stage;
    private EntityView entityView;
    private CardView cardView;
    private Judgement judgement;

    AnimeControl animeControl = new AnimeControl();
    MouseController mouseController;
    private Cleaner cleaner;

    public GameController(GameFrame gameFrame, Stage stage) {
        this.stage = stage;
        this.gameFrame = gameFrame;
        this.cleaner = new Cleaner(stage);
        this.entityCanvas = gameFrame.getEntityCanvas();

        entityView = new EntityView();
        cardView = new CardView(gameFrame.getToolsCanvas().getGraphics(), stage);
        judgement = new Judgement(stage, cleaner,cardView);
        mouseController = new MouseController(gameFrame, stage, cardView, cleaner, judgement);

    }

    public void drawGame() {
        //将所有实体列表装土entityList中
        List<Entity> entityList = stage.getAllEntityList();
        entityList.addAll(stage.getZombieFactory().getAllZombieList());
        entityList.addAll(stage.getPlantFactory().getAllPlantsList());
        entityList.addAll(stage.getBulletFactory().getAllBulletList());
        entityList.addAll(stage.getGettableFactory().getAllGettableList());

        //绘制种子栏 菜单等
        cardView.drawToolBar();

        //将实体类放入entityCanvas中进行绘制
        for (Entity entity : entityList) {
            entityCanvas.addImage(entity);
        }

    }


    public void changeCoolDown() {
        List<Entity> allEntityList = stage.getAllEntityList();

        for (int i = 0; i < allEntityList.size(); i++) {
            Entity entity = allEntityList.get(i);

            Weapon weapon = entity.getWeapon();
            if (weapon != null) {
                //改变武器冷却
                int curCoolTime = weapon.getCurCoolTime();
                if (curCoolTime < weapon.getCoolDown()) {
                    if (curCoolTime != 0 || weapon.getHasShoot()) {
                        weapon.setCurCoolTime(curCoolTime + 1);
                    }

                } else {
                    weapon.setCurCoolTime(0);
                    weapon.setHasShoot(false);
                }
            }
        }
    }

    /**
     * 游戏主体循环
     */
    public void playGame() {
        drawGame();

        //计时类
        TimeTaskPool.timerPool.scheduleAtFixedRate(() -> {
            long time = this.stage.getLevel().getLevelTime();
            this.stage.getLevel().setLevelTime(++time);
            System.out.println(time);

            //生成僵尸
            stage.getLevel().generateZombies(10);

            if (time % 15 == 0) {
                //生成阳光
                stage.getGettableFactory().generateFallingObj();
            }

            changeCoolDown();
        }, 0, 1, TimeUnit.SECONDS);

        ThreadPool.cachedThreadPool.execute(() -> {
            SoundController.playSound("bgm.wav",SoundController.LOOP);

            while (true) {
                entityView.drawEntities(stage, entityCanvas);
                judgement.entityJudge();
                judgement.attackJudge();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                entityCanvas.repaint();
            }
        });

    }
}
